package com.monitor.web.controller;

import com.alibaba.fastjson.JSON;
import com.monitor.web.dal.MobileNumberCodeDO;
import com.monitor.web.mapper.MobileNumberCodeDOMapper;
import com.monitor.web.service.MybatisPageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LoggingApplicationListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author tanlian
 * @Date 2018/12/28 15:52
 * @Desc
 */
@RestController
@RequestMapping("/sql")
@Slf4j
public class DataBaseController {

    @Autowired
    DataSource dataSource;

    @Autowired
    MobileNumberCodeDOMapper mapper;


    @Autowired
    MybatisPageHelper mybatisPageHelper;

    @RequestMapping("/test")
    public String test() throws SQLException {
        ResultSet resultSet= dataSource.getConnection().createStatement().executeQuery("select * from T_MOBILE_NUMBER_CODE");
        while (resultSet.next()){
            System.out.println(resultSet.getObject(1));
        }
        return "success";
    }

    @RequestMapping(value = "/mapper",produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MobileNumberCodeDO> mapper(){
        return mapper.selectAll();
    }

    @RequestMapping(value = "/pageHelper/{num}/{size}",produces = MediaType.APPLICATION_JSON_VALUE)
    public Object pageHelper(@PathVariable("num") int num,@PathVariable("size") int size) {
        return mybatisPageHelper.get(num, size);
    }
}
